using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Skinner
{
    public partial class ColorEdit : Form
    {
        private ColorEdit()
        {
            InitializeComponent();
        }

        private StyleSheet _sheet;
        private ColorRule _colorRule;
        private Button _colorPalletButton;

        public ColorEdit(StyleSheet sheet, ColorRule colorRule, Button colorPalletButton)
        {
            InitializeComponent();
            _sheet = sheet;
            _colorRule = colorRule;
            _colorPalletButton = colorPalletButton;
            AddClassLinks();
            txtColor.Text = colorRule.NewColorName;

            SetButtonColor(_colorRule.NewColorName);
        }

        private void SetButtonColor(string colorName)
        {
            try
            {
                btnChoose.BackColor = System.Drawing.Color.FromArgb(int.Parse(colorName.Substring(1, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(colorName.Substring(3, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(colorName.Substring(5, 2), System.Globalization.NumberStyles.HexNumber));
            }
            catch
            {
                try
                {
                    btnChoose.BackColor = System.Drawing.Color.FromName(colorName);
                }
                catch { }
            }
        }

        private void AddClassLinks()
        {
            int height = 20;
            int topPad = 1;
            int leftPad = 5;
            int topOffset = 0;

            int position = 0;

            foreach(string rule in _colorRule.RuleSelectors)
            {
                LinkLabel label = new LinkLabel();
                label.Text = rule;
                label.AutoSize = true;
                label.Location = new Point(leftPad, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                position++;
                label.Click += new EventHandler(classLabel_Click);
                classPanel.Controls.Add(label);
            }
        }

        void classLabel_Click(object sender, EventArgs e)
        {
            StyleEdit editForm = new StyleEdit(_sheet.Rules[((LinkLabel)sender).Text.ToUpper()]);
            editForm.Show();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            Button btnColor = (Button)sender;
            colorDialog.Color = btnColor.BackColor;
            colorDialog.FullOpen = true;
            if (colorDialog.ShowDialog(btnColor) == DialogResult.OK) 
            {
                btnColor.BackColor = colorDialog.Color;
                txtColor.Text = System.Drawing.ColorTranslator.ToHtml(colorDialog.Color);
            }
        }

        private void txtColor_TextChanged(object sender, EventArgs e)
        {
            SetButtonColor(txtColor.Text);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string replaceName;

                //If the color was already changed we need to use the current name in the replacement, not the original.
                if (_colorRule.ColorName == _colorRule.NewColorName)
                    replaceName = _colorRule.ColorName;
                else
                    replaceName = _colorRule.NewColorName;

                foreach (string ruleName in _colorRule.RuleSelectors)
                {
                    StyleRule rule = _sheet.Rules[ruleName.ToUpper()];

                    _colorRule.NewColorName = txtColor.Text.ToLower();
                    foreach (mshtml.IHTMLStyleSheetRule innerRule in rule.Rules)
                    {
                        innerRule.style.cssText = innerRule.style.cssText.ToLower().Replace(replaceName.ToLower(), _colorRule.NewColorName.ToLower());
                    }
                }

                _colorPalletButton.BackColor = btnChoose.BackColor;
                _colorPalletButton.Text = _colorRule.NewColorName;

                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(string.Format("An error occured setting the styles. Please verify your syntax. Error {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }
    }
}